package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.configInfo.ConfigInfo;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.constant.types.Whether;
import com.hornsun.domain.dbo.Information;
import com.hornsun.domain.dbo.InformationExample;
import com.hornsun.repository.mapper.InformationMapper;
import com.hornsun.util.LittleUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/2/26.
 */
@Repository
public class InformationRepository {

    @Resource
    private InformationMapper informationMapper;

    @Resource
    private ConfigInfo configInfo;

    @Resource
    private LittleUtil littleUtil;

    public List<Information> getRecommendInformationByType(String typeid){
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid).andRecommendEqualTo((byte)Whether.YES.getType().intValue());
        return informationMapper.selectByExample(informationExample);
    }

    public List<Information>  getInformationByTypeNotRecommend(String typeid,int page){
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause("update_time");
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid).andRecommendEqualTo((byte)Whether.NOT.getType().intValue());
        return informationMapper.selectByExampleWithRowbounds(informationExample,littleUtil.littleRowBounds(page));
    }

    public List<Information> getLastInformationByType(String typeid){
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid);
        return informationMapper.selectByExampleWithRowbounds(informationExample,new RowBounds(0,1));
    }
    public List<Information> getInformationExLast(String typeid,int page,String guid){
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid).andInformationGuidNotEqualTo(guid);
        return informationMapper.selectByExampleWithRowbounds(informationExample,littleUtil.littleRowBounds(page));
    }

    public void addInformation(Information information){
        int count = informationMapper.insertSelective(information);
        if (count!=1){
            throw new IllegalArgumentException("insert information failed by information:"+information);
        }
    }

    public void updateInformation(Information information){
        int count = informationMapper.updateByPrimaryKeySelective(information);
        if (count!=1){
            throw new IllegalArgumentException("update information failed by information:"+information);
        }
    }

    public int deleteInformation(String guid){
        return informationMapper.deleteByPrimaryKey(guid);
    }

    public void resetRecommend (String typeid){
        informationMapper.resetRecommend(typeid);
    }

    public  List<Information> getHomeRecommendInformation(){
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andHomepangRecommendEqualTo((byte) Whether.YES.getType().intValue());
        return informationMapper.selectByExample(informationExample);
    }

    //获取收藏资讯信息
    public List<Information> getFavouriteInformation(String openid,int page){
        Preconditions.checkNotNull(openid);
        Preconditions.checkNotNull(page);
        int offset  = (page-1)*8;
        return informationMapper.getFavouriteInformation(openid,offset);
    }



    //获取最近两条资讯信息
    public List<Information> getLatelyTwo(){
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        return informationMapper.selectByExampleWithRowbounds(informationExample,new RowBounds(0,2));
    }


    //获取资讯总数
    public long getCountByTypeid(String typeid){
        Preconditions.checkNotNull(typeid);
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid);
       return informationMapper.countByExample(informationExample);
    }
    //根据type获取资讯信息
    public List<Information>  getInformationByType(String typeid,int page){
        Preconditions.checkNotNull(typeid);
        Preconditions.checkNotNull(page);
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeid);
        return informationMapper.selectByExampleWithRowbounds(informationExample,littleUtil.littleRowBounds(page));
    }

    //根据guid获取资讯信息
    public Information getInformationByGuid(String guid){
        Preconditions.checkNotNull(guid);
        return informationMapper.selectByPrimaryKey(guid);
    }

    //点赞
    public void clickPraise(String informationGuid){
        Preconditions.checkNotNull(informationGuid);
        informationMapper.updatePraiseByGuid(informationGuid);
    }

    //点击量
    public void click(String informationGuid){
        Preconditions.checkNotNull(informationGuid);
        informationMapper.updateHitsByGuid(informationGuid);
    }

    //contentType下是否有内容
    public boolean hasContentByTypeId(String typeId){
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andTypeidEqualTo(typeId);
        long count = informationMapper.countByExample(informationExample);
        if (count>0){
            return true;
        }
        return false;
    }

    //获取总条数
    public long getTotalCount(){
        return informationMapper.countByExample(new InformationExample());
    }

    //获取所有
    public List<Information> getAll(int page){
        InformationExample informationExample =  new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        return informationMapper.selectByExampleWithRowbounds(informationExample,littleUtil.littleRowBounds(page));
    }

    //模糊查询
    public List<Information> fuzzyQuery(String description,int page){
        InformationExample informationExample = new InformationExample();
        informationExample.setOrderByClause(StrUtil.toDesc("update_time"));
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andDescriptionLike(description);
        return informationMapper.selectByExampleWithRowbounds(informationExample,littleUtil.littleRowBounds(page));
    }

    //模糊查询的数量
    public long countByFuzzyQuery(String description){
        InformationExample informationExample = new InformationExample();
        InformationExample.Criteria criteria = informationExample.createCriteria();
        criteria.andDescriptionLike(description);
        return informationMapper.countByExample(informationExample);
    }

    //更改资讯收藏数
    public void updateFavourite(String informationGuid){
        Preconditions.checkNotNull(informationGuid);
        informationMapper.updateFavourite(informationGuid);
    }



}
